alter proc spThemCTNhapHang (@maNhapHang int, @maNL int, @soLuong float, @donGia int, @tong int, @maKho int)
as
begin
	Insert Into ChiTietNhapHang
	(
		MaNhapHang,
		MaNL,
		SoLuong,
		DonGia,
		Tong
	)
	Values
	(
		@maNhapHang,
		@maNL,
		@soLuong,
		@donGia,
		@tong
	)
	DECLARE @luongTon float
	DECLARE @toiThieu int
	DECLARE @tinhTrang int
	select @luongTon = LuongTon, @toiThieu = SucChuaThapNhat
	from ChiTietKho
	where MaKho = @maKho and MaNL = @maNL

	set @luongTon = @luongTon + @soLuong
	if (@luongTon > @toiThieu)
		set @tinhTrang = 2
	else if (@luongTon = 0)
		set @tinhTrang = 0
	else set @tinhTrang = 1

	update ChiTietKho
	Set
			LuongTon = @luongTon,
			TinhTrang = @tinhTrang
		Where MaKho = @maKho and MaNL = @maNL
end